import cookies from 'js-cookie'
import ManageConstant from "../consts/manageConstant";
import qs from "qs"

class RoleService {

  constructor(_this) {
    this._this = _this;
    this._axios = _this.axios.create({
      baseURL: ManageConstant.DOMAIN_URL,
      timeout: 5000,
      headers: {
        Token: cookies.get('Token')
      }
    });
  }

  searchRole(params, executeMethod){
    this._axios.get(ManageConstant.ROLE_SEARCH, {params}).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

  addRole(params){
    this._axios.post(ManageConstant.ROLE_ADD, params).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("添加角色成功");
        this._this.resetFormData();
        this._this.searchRole(0,10,'');
      }
      else if(res.data.resultCode == 1006){
        this._this.$Message.error("角色已存在");
      }
      else{
        this._this.$Message.error("添加失败");
      }
    })
  }

  editRole(params){
    this._axios.post(ManageConstant.ROLE_EDIT, params).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("修改角色成功");
        this._this.resetFormData();
        this._this.searchRole(0,10,'');
      }
      else{
        this._this.$Message.error("修改失败");
      }
    })
  }
  //注意使用qs传数组数据，参数名要与文档要求参数名一致如下roleIds
  batchDeleteRoles(roleIds){
    this._axios.post(ManageConstant.ROLE_BATCH_DELETE, qs.stringify({
      roleIds
    }, {
      indices: false
    })).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("删除角色成功");
        this._this.resetFormData();
        this._this.searchRole(0,10,'');
      }
      else{
        this._this.$Message.error("删除失败");
      }
    })
  }

}

export default RoleService;
